<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-select class="filter-item" style="width: 160px" size="mini" v-model="form.searchType" @change="handlePageInit">
        <el-option v-for="item in pageData.searchTypeList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
<!--      <el-button class="filter-item" type="primary" size="mini" icon="el-icon-edit" @click="handleOpenSavePanel">添加勋章</el-button>-->
    </div>
    <!-- 页面数据列表 -->
    <el-table :data="responseData.tableList"
              border style="width: 100%;"
              v-loading="pageData.loading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(0, 0, 0, 0.8)"
              :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
              @selection-change="handleTableSelection">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column fixed="left" prop="medalId" label="规则ID" width="280" align="center"/>
      <el-table-column prop="medalName" label="勋章名称" width="180" align="center">
        <template slot-scope="scope">
          <span><el-tag type="primary">{{scope.row.medalName}}</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="medalLevel" label="等级编号" width="180" align="center"/>
      <el-table-column prop="medalLevelLowCount" label="开始经验" width="180" align="center"/>
      <el-table-column prop="medalLevelMaxCount" label="结束经验" width="180" align="center"/>
      <el-table-column prop="medalLevelCount" label="升级经验" width="180" align="center"/>
      <el-table-column prop="medalType" label="勋章类型" width="180" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.medalType === 1"><el-tag type="info">连续签到勋章</el-tag></span>
          <span v-if="scope.row.medalType === 2"><el-tag type="success">阅读文案勋章</el-tag></span>
          <span v-if="scope.row.medalType === 3"><el-tag type="primary">观看视频勋章</el-tag></span>
          <span v-if="scope.row.medalType === 4"><el-tag type="warning">收听音频勋章</el-tag></span>
          <span v-if="scope.row.medalType === 5"><el-tag type="info">问答提问勋章</el-tag></span>
          <span v-if="scope.row.medalType === 6"><el-tag type="success">问答回答勋章</el-tag></span>
          <span v-if="scope.row.medalType === 7"><el-tag type="primary">内容评论勋章</el-tag></span>
          <span v-if="scope.row.medalType === 8"><el-tag type="warning">分享内容勋章</el-tag></span>
          <span v-if="scope.row.medalType === 13"><el-tag type="info">点赞留痕勋章</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="medalStatus" label="审核状态" width="120" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.medalStatus === 0"><el-tag type="info">线下</el-tag></span>
          <span v-if="scope.row.medalStatus === 1"><el-tag type="success">线上</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="medalInfo" label="勋章简介" width="360" align="center"/>
      <el-table-column fixed="right" label="操作" width="240" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleOpenEditPanel(scope)">编辑</el-button>
          <el-button type="danger" size="mini"  @click="handleDeleteData(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="form.pageNum"
      :page-size="form.pageSize"
      :total="form.total"
      :page-sizes="[12, 50, 100, 200]"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange">
    </el-pagination>
    <!-- 新增/修改隐藏域 -->
    <el-dialog title="勋章配置" :visible.sync="pageData.savePanelStatus" size="small"
               :before-close="handleCloseSavePanel">
      <div>
        <el-form ref="form.medal" :model="form.medal" label-width="120px">
          <el-form-item label="勋章名称">
            <el-input v-model="form.medal.medalName"></el-input>
          </el-form-item>
          <el-form-item label="勋章图像">
            <el-upload
              class="avatar-uploader"
              action="/api/cms/common/uploadFile"
              :show-file-list="false"
              :before-upload="handleHomeImageUploadBefore"
              :on-success="handleHomeImageUploadSuccess">
              <img v-if="form.medal.medalImage" :src="form.medal.medalImage" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="勋章类型">
            <el-radio-group v-model="form.medal.medalType" @change="handleMedalTypeChange">
              <el-radio :label="1">连续签到勋章</el-radio>
              <el-radio :label="2">阅读文案勋章</el-radio>
              <el-radio :label="3">观看视频勋章</el-radio>
              <el-radio :label="4">收听音频勋章</el-radio>
              <el-radio :label="5">问答提问勋章</el-radio>
              <el-radio :label="6">问答回答勋章</el-radio>
              <el-radio :label="7">内容评论勋章</el-radio>
              <el-radio :label="8">分享内容勋章</el-radio>
              <el-radio :label="13">点赞留痕勋章</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="勋章简介">
            <el-input v-model="form.medal.medalInfo"></el-input>
          </el-form-item>
          <el-form-item label="审核状态">
            <el-radio-group v-model="form.medal.medalStatus">
              <el-radio :label="0">线下</el-radio>
              <el-radio :label="1">线上</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="等级编号">
            <el-input v-model="form.medal.medalLevel" disabled="disabled"></el-input>
          </el-form-item>
          <el-form-item label="开始经验">
            <el-input v-model="form.medal.medalLevelLowCount" disabled="disabled"></el-input>
          </el-form-item>
          <el-form-item label="结束经验">
            <el-input v-model="form.medal.medalLevelMaxCount" disabled="disabled"></el-input>
          </el-form-item>
          <el-form-item label="升级经验">
            <el-input v-model="form.medal.medalLevelCount" @change="handleMedalLevelCountChange"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveButtonClick">保存勋章</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsMedalList,loadCmsMedalMaxData,saveCmsMedalData,updateCmsMedalData,deleteCmsMedalData } from "@/api/operation";
  export default {
    name: "medal",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          searchTypeList: [
            {label: "请选择勋章类型", value: -1},
            {label: "连续签到勋章", value: 1},
            {label: "阅读文案勋章", value: 2},
            {label: "观看视频勋章", value: 3},
            {label: "收听音频勋章", value: 4},
            {label: "问答提问勋章", value: 5},
            {label: "问答回答勋章", value: 6},
            {label: "内容评论勋章", value: 7},
            {label: "分享内容勋章", value: 8},
            {label: "点赞留痕勋章", value: 13},
          ],
          loading: false,
          savePanelStatus: false,
          savePanelType: 0
        },// 页面配置数据
        form:{
          searchType: -1,
          pageNum: 1,// 表单--页码
          pageSize: 12,// 表单--页面
          total: 0,// 表单--总量
          selection: [],// 多选的内容
          medal: {
            medalId: "",
            medalName: "",
            medalImage: "",
            medalType: 1,
            medalInfo: "",
            medalStatus: 0,
            medalLevel: 0,
            medalLevelLowCount: 0,
            medalLevelMaxCount: 0,
            medalLevelCount: 0
          },
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: []
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsMedalList();// 读取路由角色列表
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.handlePageInit();// 进行数据加载
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.handlePageInit();// 进行数据加载
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].medalId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePanel(){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "260001")){
          this.loadCmsMedalMaxData();
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开新增面板
      handleCloseSavePanel(){
        this.$confirm("确认关闭？").then(response => {
          // 初始化数据
          this.form.medal.medalId = "";
          this.form.medal.medalName = "";
          this.form.medal.medalImage = "";
          this.form.medal.medalType = 0;
          this.form.medal.medalInfo = "";
          this.form.medal.medalStatus = 0;
          this.form.medal.medalLevel = 0;
          this.form.medal.medalLevelLowCount = 0;
          this.form.medal.medalLevelMaxCount = 0;
          this.form.medal.medalLevelCount = 0;
          // 打开面板
          this.pageData.savePanelStatus = false;
          this.pageData.savePanelType = 0;
        }).catch(err => {
          console.log(err);
        });
      },// 关闭新增面板
      handleOpenEditPanel(scope){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "260003")){
          // 将等级数据最新等级数据补充到表单中
          this.form.medal.medalId = scope.row.medalId;
          this.form.medal.medalName = scope.row.medalName;
          this.form.medal.medalImage = scope.row.medalImage;
          this.form.medal.medalType = scope.row.medalType;
          this.form.medal.medalInfo = scope.row.medalInfo;
          this.form.medal.medalStatus = scope.row.medalStatus;
          this.form.medal.medalLevel = scope.row.medalLevel;
          this.form.medal.medalLevelLowCount = scope.row.medalLevelLowCount;
          this.form.medal.medalLevelMaxCount = scope.row.medalLevelMaxCount;
          this.form.medal.medalLevelCount = scope.row.medalLevelCount;
          // 打开面板
          this.pageData.savePanelStatus = true;
          this.pageData.savePanelType = 2;
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开修改面板
      handleMedalTypeChange(){
        this.loadCmsMedalMaxData();
      },// 类型筛选事件
      handleMedalLevelCountChange(){
        // 通过修改升级经验值，自动获取当前最大经验值
        this.form.expert.expLevelMaxCount = parseInt(this.form.expert.expLevelLowCount) + parseInt(this.form.expert.expLevelCount);
      },// 检测升级经验是否合规
      handleHomeImageUploadBefore(file){
        const fileSize = file.size / 1024 / 1024 < 2;
        if (!fileSize) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return fileSize;
      },//图片上传之前的验证
      handleHomeImageUploadSuccess(response){
        this.form.medal.medalImage = response.data;
      },// 图片上传成功的方法
      handleSaveButtonClick(){
        // 判断当前登录用户所拥有的的操作权限
        if (this.pageData.savePanelType === 1) {
          if (!PermissionsToDetectFormUser(getPermission(), "260001")){
            this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
            return false;
          }
        } else if (this.pageData.savePanelType === 2) {
          if (!PermissionsToDetectFormUser(getPermission(), "260003")){
            this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
            return false;
          }
        }
        // 当前操作权限充足，进行敏感操作提示
        this.$confirm('此操作将会保存当前数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 同一操作，执行具体流程
          if (this.pageData.savePanelType === 1) {
            this.saveCmsMedalData();
          } else if (this.pageData.savePanelType === 2) {
            this.updateCmsMedalData();
          }
        }).catch(close => {
          console.log(close);
          this.$message.info('已取消操作');
        });
      },// 保存勋章配置数据
      handleDeleteData(scope){
        // 判断当前登录用户所拥有的的操作权限
        if (PermissionsToDetectFormUser(getPermission(), "260002")){
          // 当前操作权限充足，进行敏感操作提示
          this.$confirm('此操作将会删除当前数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 同一操作，执行具体流程
            let param = {
              medalId: scope.row.medalId,
              medalLevel: scope.row.medalLevel
            };
            this.deleteCmsMedalData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info('已取消操作');
          });
        } else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 删除按钮点击事件
      // 接口
      loadCmsMedalList(){
        loadCmsMedalList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取数据列表
      loadCmsMedalMaxData(){
        let prams = { searchType: this.form.searchType === -1 ? 1 : this.form.searchType };
        loadCmsMedalMaxData(prams).then(response => {
          if (response.code === "200"){
            // 将等级数据最新等级数据补充到表单中
            this.form.medal.medalId = response.data.medalId;
            this.form.medal.medalName = response.data.medalName;
            this.form.medal.medalImage = response.data.medalImage;
            this.form.medal.medalType = response.data.medalType;
            this.form.medal.medalInfo = response.data.medalInfo;
            this.form.medal.medalStatus = response.data.medalStatus;
            this.form.medal.medalLevel = parseInt(response.data.medalLevel) + 1;
            this.form.medal.medalLevelLowCount = parseInt(response.data.medalLevelLowCount) + 1;
            this.form.medal.medalLevelMaxCount = parseInt(response.data.medalLevelMaxCount) + parseInt(response.data.medalLevelCount);
            this.form.medal.medalLevelCount = parseInt(response.data.medalLevelCount);
            // 打开面板
            this.pageData.savePanelStatus = true;
            this.pageData.savePanelType = 1;
          } else {
            this.$message.warning(response.message);
          }
        }).catch(error => {
          console.log(error);
          this.$message.error(error);
        });
      },// 获取勋章最大等级配置值
      saveCmsMedalData(){
        saveCmsMedalData(this.form.medal).then(response => {
          if (response.code === "200"){
            // 初始化数据
            this.form.medal.medalId = "";
            this.form.medal.medalName = "";
            this.form.medal.medalImage = "";
            this.form.medal.medalType = 0;
            this.form.medal.medalInfo = "";
            this.form.medal.medalStatus = 0;
            this.form.medal.medalLevel = 0;
            this.form.medal.medalLevelLowCount = 0;
            this.form.medal.medalLevelMaxCount = 0;
            this.form.medal.medalLevelCount = 0;
            // 打开面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            this.handlePageInit()
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 保存勋章数据
      updateCmsMedalData(){
        updateCmsMedalData(this.form.medal).then(response => {
          if (response.code === "200"){
            // 初始化数据
            this.form.medal.medalId = "";
            this.form.medal.medalName = "";
            this.form.medal.medalImage = "";
            this.form.medal.medalType = 0;
            this.form.medal.medalInfo = "";
            this.form.medal.medalStatus = 0;
            this.form.medal.medalLevel = 0;
            this.form.medal.medalLevelLowCount = 0;
            this.form.medal.medalLevelMaxCount = 0;
            this.form.medal.medalLevelCount = 0;
            // 打开面板
            this.pageData.savePanelStatus = false;
            this.pageData.savePanelType = 0;
            this.handlePageInit()
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 更新勋章数据
      deleteCmsMedalData(param){
        deleteCmsMedalData(param).then(response => {
          if (response.code === "200"){
            // 初始化数据
            this.$message.success("删除成功");
            this.handlePageInit()
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 删除勋章数据
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
